﻿namespace System.ServiceModel.DomainServices.Client
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.ComponentModel.DataAnnotations;

    public sealed class DomainClientResult
    {
        private ReadOnlyCollection<Entity> _allEntities;
        private ReadOnlyCollection<Entity> _entities;
        private object _returnValue;
        private int _totalEntityCount;
        private IEnumerable<ValidationResult> _validationErrors;

        private DomainClientResult()
        {
        }

        private DomainClientResult(object returnValue, IEnumerable<ValidationResult> validationErrors)
        {
            this._returnValue = returnValue;
            this._validationErrors = validationErrors.ToList<ValidationResult>().AsReadOnly();
        }

        private DomainClientResult(IEnumerable<Entity> entities, IEnumerable<Entity> allEntities, int totalEntityCount, IEnumerable<ValidationResult> validationErrors)
        {
            this._entities = entities.ToList<Entity>().AsReadOnly();
            this._allEntities = allEntities.ToList<Entity>().AsReadOnly();
            this._totalEntityCount = totalEntityCount;
            this._validationErrors = validationErrors.ToList<ValidationResult>().AsReadOnly();
        }

        public static DomainClientResult CreateInvokeResult(object returnValue, IEnumerable<ValidationResult> validationErrors)
        {
            if (validationErrors == null)
            {
                throw new ArgumentNullException("validationErrors");
            }
            return new DomainClientResult(returnValue, validationErrors);
        }

        public static DomainClientResult CreateQueryResult(IEnumerable<Entity> entities, IEnumerable<Entity> allEntities, int totalEntityCount, IEnumerable<ValidationResult> validationErrors)
        {
            if (entities == null)
            {
                throw new ArgumentNullException("entities");
            }
            if (allEntities == null)
            {
                throw new ArgumentNullException("allEntities");
            }
            if (validationErrors == null)
            {
                throw new ArgumentNullException("validationErrors");
            }
            return new DomainClientResult(entities, allEntities, totalEntityCount, validationErrors);
        }

        public IEnumerable<Entity> AllEntities
        {
            get
            {
                return this._allEntities;
            }
        }

        public IEnumerable<Entity> Entities
        {
            get
            {
                return this._entities;
            }
        }

        public object ReturnValue
        {
            get
            {
                return this._returnValue;
            }
        }

        public int TotalEntityCount
        {
            get
            {
                return this._totalEntityCount;
            }
        }

        public IEnumerable<ValidationResult> ValidationErrors
        {
            get
            {
                return this._validationErrors;
            }
        }
    }
}

